from lxml import html
import random


class Comment:
    genders = ['f', 'm']

    def __init__(self, data):
        self.comment = {}
        self.comment['user'] = {}
        self.comment['timestamp'] = data['created_at']  # 发表日期时间
        if 'like_count' in data:
            self.comment['like_count'] = data['like_count']  # 点赞数
        else:
            self.comment['like_count'] = data['like_counts']
        text = data['text']  # 评论内容
        tree = html.fromstring(text)
        self.comment['content'] = tree.xpath('string(.)')  # 用string函数过滤掉多余标签

        self.comment['user']['id'] = data['user']['id']
        self.comment['user']['screen_name'] = data['user']['screen_name']
        if 'gender' in data['user']:
            self.comment['user']['gender'] = data['user']['gender']
        else:
            self.comment['user']['gender'] = random.choice(self.genders)
        self.comment['user']['followers_count'] = data['user']['followers_count']
        self.comment['user']['verified_type'] = data['user']['verified_type']

        source = data['source']  # 来源于哪个终端
        if source == '':
            self.comment['source'] = u'未知'

        # 评论内容的图片
        if 'pic' in data:
            self.comment['source'] = data['pic']['url']
        else:
            self.comment['source'] = ""
